/* 
  index.js
 */ 

var baseUrl = 'http://wm0706-api.weimansoft.vip'
function GetQueryString(name){
   var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
   var r = window.location.search.substr(1).match(reg);
   if(r!=null)return  unescape(r[2]); return null;
}
var vm = new Vue({
    el: '#app',
    data: {
      downloadUrl: '',
      email:'',
      code:'',
      timeout: 0,
      password:'',
      password1:'',
      inviteCode: '',
      tips: ''
    },
    created() {
      this.inviteCode = GetQueryString('code')
      axios.get(baseUrl + '/api/app/download').then(res=>{
        if(res.data.status_code == 200){
          this.downloadUrl = res.data.data.link
        }
      })
    },
    methods:{
      toast(msg){
        this.tips = msg;
        setTimeout(()=> {
          this.tips = ''
        },1500)
      },
      getCode(){
        let _this = this
        if(!this.email){
          this.toast('请输入邮箱')
          return
        }
        axios.post(baseUrl+ '/api/send_email',{
          email: this.email,
          from: 'register'
        }).then(res=>{
          if(res.data.status_code == 200){
            this.toast('发送成功')
            this.timeout = 60;
            let t = setInterval(()=>{
              if(this.timeout){
                this.timeout--
              } else {
                clearInterval(t)
              }
            },1000)
          }else {
            this.toast(res.data.message)
          }
        })
      },
      submit(){
        let _this = this
        if(!this.email){
          this.toast('请输入邮箱')
          return
        }
        let data = {
          username: this.email,
          v_code: this.code,
          invite_code: this.inviteCode,
          password: this.password,
          password_confirmation: this.password1
        }
        axios.post(baseUrl+ '/api/register',data,{
          // headers:{
          //   'timestamp':date
          // }
        }).then(res=>{
          if(res.data.status_code == 200){
            this.toast('注册成功')
            setTimeout(()=>{
              location.href = this.downloadUrl;
            },1000)
          }else {
            this.toast(res.data.message)
          }
        })
      }
    }
  })